package com.kuangstudy.service.set;

import com.kuangstudy.entity.User;
import com.kuangstudy.service.UserService;
import com.kuangstudy.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2021/5/24 16:53
 */
@Service
public class FollowerServicee {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    // 关注列表
    public static final String FOLLOWEE_SET_KEY = "followee:user:";
    // 粉丝列表
    public static final String FOLLOWER_SET_KEY = "follower:user:";

    /**
     * 飞哥关注狂神
     *
     * @param userid=飞哥
     * @param followid=狂神
     */
    public void follow(Integer userid, Integer followid) {
        // 1: 获取redis的集合对象
        SetOperations<String, Integer> setOperations = redisTemplate.opsForSet();
        // 2:飞哥关注集合
        String followeekey = FOLLOWEE_SET_KEY + userid;
        setOperations.add(followeekey, followid);
        // 3:狂神粉丝集合
        String followerkey = FOLLOWER_SET_KEY + followid;
        setOperations.add(followerkey, userid);
    }


    /**
     * 查询我的关注
     *
     * @param userid=飞哥
     */
    public List<User> listMyfollowee(Integer userid) {
        // 1: 获取redis的集合对象
        SetOperations<String, Integer> setOperations = redisTemplate.opsForSet();
        // 2:飞哥关注集合
        String followeekey = FOLLOWEE_SET_KEY + userid;
        Set<Integer> members = setOperations.members(followeekey);
        // 3: 把关注用户的信息从hash中获取出来
        return getUserInfos(members);
    }

    /**
     * 我的粉丝列表
     *
     * @param userid=飞哥
     */
    public List<User> listMyfollower(Integer userid) {
        // 1: 获取redis的集合对象
        SetOperations<String, Integer> setOperations = redisTemplate.opsForSet();
        // 2:飞哥关注集合
        String followerkey = FOLLOWER_SET_KEY + userid;
        Set<Integer> members = setOperations.members(followerkey);
        // 3: 把关注用户的信息从hash中获取出来
        return getUserInfos(members);
    }

    private List<User> getUserInfos(Set<Integer> userinfos) {
        // 1：创建用户集合
        List<User> userList = new ArrayList<>();

        // 2: 需要从hash中获取的属性
        List<String> hasKeys = new ArrayList<>();
        hasKeys.add("id");
        hasKeys.add("nickname");

        HashOperations opsForHash = this.redisTemplate.opsForHash();
        // 2：循环关注列表的用户ID信息
        for (Integer userId : userinfos) {
            // 3：获取用户在hash中的注册完整信息对应的key
            String hkey = "reg:user:" + userId;
            List<Object> list = opsForHash.multiGet(hkey, hasKeys);
            // 4：如果在缓存中没有找到对应的用户信息
            if (list.get(0) == null && list.get(1) == null) {
                User user = this.getUserDbCache(userId);
                userList.add(user);
            }else{
                User user = new User();
                user.setId(Integer.valueOf(list.get(0).toString()));
                user.setNickname(list.get(1).toString());
                userList.add(user);
            }
        }

        return userList;
    }

    public User getUserDbCache(Integer userid) {
        // 1： 然后查询最新的用户信息放入到redis的hash重
        User user1 = userService.getById(userid);
        Map<String, Object> map = R.beanToMap(user1);
        // 3: 准备用存入的key，将用户信息存入到redis的hash中
        String key = "reg:user:" + user1.getId();
        redisTemplate.opsForHash().putAll(key, map);
        // 4: 设置key的失效时间一个月
        redisTemplate.expire(key, 30, TimeUnit.DAYS);
        return user1;
    }


}
